#include "NewDeviceDialog.h"
#include "ui_NewDeviceDialog.h"
#include <QDir>
NewDeviceDialog::NewDeviceDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NewDeviceDialog)
{
    ui->setupUi(this);
    setWindowTitle("新建采集设备");
    ui->comboBox->setEditable(true);
    ui->comboBox_3->setEditable(true);
    QWidget * tcpClientWidget = new QWidget();
    QVBoxLayout * vLayout = new QVBoxLayout();
    QHBoxLayout * hLayout = new QHBoxLayout();
    {
        QLabel * label1 = new QLabel("IP地址A");
        QLineEdit * edit1 = new QLineEdit();
        m_mapChilds.insert("ipaddrA",edit1);
        QLabel * label2 = new QLabel("端口号A");
        QLineEdit * edit2 = new QLineEdit();
        m_mapChilds.insert("portA",edit2);
        hLayout->addWidget(label1);
        hLayout->addWidget(edit1);
        hLayout->addWidget(label2);
        hLayout->addWidget(edit2);
        vLayout->addLayout(hLayout);
    }
    QHBoxLayout * hLayout1 = new QHBoxLayout();
    {
        QLabel * label1 = new QLabel("IP地址B");
        QLineEdit * edit1 = new QLineEdit();
        m_mapChilds.insert("ipaddrB",edit1);
        QLabel * label2 = new QLabel("端口号B");
        QLineEdit * edit2 = new QLineEdit();
        m_mapChilds.insert("portB",edit2);
        hLayout1->addWidget(label1);
        hLayout1->addWidget(edit1);
        hLayout1->addWidget(label2);
        hLayout1->addWidget(edit2);
        vLayout->addLayout(hLayout1);
    }
    tcpClientWidget->setLayout(vLayout);
    ui->stackedWidget->addWidget(tcpClientWidget);
    //tcpServer配置
    QWidget * tcpServerWidget = new QWidget();
    QHBoxLayout * hLayout3 = new QHBoxLayout();
    {
        QLabel * label1 = new QLabel("服务端IP地址");
        QLineEdit * edit1 = new QLineEdit();
        m_mapChilds.insert("serverip",edit1);
        QLabel * label2 = new QLabel("服务端端口号");
        QLineEdit * edit2= new QLineEdit();
        m_mapChilds.insert("serverport",edit2);
        hLayout3->addWidget(label1);
        hLayout3->addWidget(edit1);
        hLayout3->addWidget(label2);
        hLayout3->addWidget(edit2);
    }
    tcpServerWidget->setLayout(hLayout3);
    ui->stackedWidget->addWidget(tcpServerWidget);

    //UDP配置
    QWidget * udpWidget = new QWidget();
    QVBoxLayout * vLayout6 = new QVBoxLayout();
    {
        QHBoxLayout * hLayout4 = new QHBoxLayout();
        QHBoxLayout * hLayout5 = new QHBoxLayout();
        QLabel * label1 = new QLabel("远程IP");
        QLineEdit * edit1 = new QLineEdit();
        m_mapChilds.insert("updDestIpaddr",edit1);
        QLabel * label2 = new QLabel("远程端口");
        QLineEdit * edit2= new QLineEdit();
        m_mapChilds.insert("updDestPort",edit2);
        QLabel * label3 = new QLabel("本地IP");
        QLineEdit * edit3 = new QLineEdit();
        m_mapChilds.insert("updSrcIpaddr",edit3);
        QLabel * label4 = new QLabel("本地端口");
        QLineEdit * edit4= new QLineEdit();
        m_mapChilds.insert("updSrcPort",edit4);

        hLayout4->addWidget(label1);
        hLayout4->addWidget(edit1);
        hLayout4->addWidget(label2);
        hLayout4->addWidget(edit2);

        hLayout5->addWidget(label3);
        hLayout5->addWidget(edit3);
        hLayout5->addWidget(label4);
        hLayout5->addWidget(edit4);

        vLayout6->addLayout(hLayout4);
        vLayout6->addLayout(hLayout5);
    }
    udpWidget->setLayout(vLayout6);
    ui->stackedWidget->addWidget(udpWidget);
    //串口
    QWidget * commWidget = new QWidget();
    QVBoxLayout * vLayout7 = new QVBoxLayout();
    {
        QLabel * label1 = new QLabel("设备为串口时，使用通道上配置串口的参数");
        vLayout7->addWidget(label1);
    }
    commWidget->setLayout(vLayout7);
    ui->stackedWidget->addWidget(commWidget);




}

NewDeviceDialog::~NewDeviceDialog()
{
    delete ui;
}

void NewDeviceDialog::SetInitDevicelName(QString name, int channelID)
{
    ui->lineEdit->setText(name);
    ui->lineEdit_2->setText("采集设备");
    QString protoName = ModelXml::GetInstance()->GetCollectChannel(channelID).protoName.c_str();
    QString path = QDir::currentPath()+"/Collect/"+protoName +"/protocal.xml";
    m_params.clear();
    ParseXML(path);

    ui->tableWidget->horizontalHeader();
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->setRowCount(m_params.size());
    QMap<int,stPrivateParam>::iterator it = m_params.begin();
    for(int row = 0 ; it != m_params.end(); it++,row++)
    {
        QTableWidgetItem * pItem1 = new QTableWidgetItem(QString::number(it->id));
        pItem1->setFlags(Qt::ItemIsEnabled);
        pItem1->setBackgroundColor(QColor(192,192,192));
        ui->tableWidget->setItem(row,0, pItem1);
        QTableWidgetItem * pItem2 = new QTableWidgetItem(it->name.c_str());
        pItem2->setFlags(Qt::ItemIsEnabled);
        pItem2->setBackgroundColor(QColor(192,192,192));
        ui->tableWidget->setItem(row,1, pItem2);
        QTableWidgetItem * pItem3 = new QTableWidgetItem(it->desc.c_str());
        pItem3->setFlags(Qt::ItemIsEnabled);
        pItem3->setBackgroundColor(QColor(192,192,192));
        ui->tableWidget->setItem(row,2,pItem3);

        ui->tableWidget->setItem(row,3,new QTableWidgetItem(it->value.c_str()));
    }

    stCollectChannel st =  ModelXml::GetInstance()->GetCollectChannel(channelID);
    if(st.portType == stCollectChannel::Port_Invented)
    {
        ui->comboBox_2->setEnabled(true);
        ui->comboBox_2->removeItem(3);
    }else
    {
        if(ui->comboBox_2->count() < 3)
        {
            ui->comboBox_2->addItem("串口");
        }
        ui->comboBox_2->setCurrentIndex(3);
        ui->comboBox_2->setDisabled(true);
    }
}

stCollecDevice NewDeviceDialog::GetDeviceInfo()
{
    m_collectDevice.name = ui->lineEdit->text().toStdString();
    m_collectDevice.desc = ui->lineEdit_2->text().toStdString();
    m_collectDevice.devAddr = ui->comboBox->currentText().toStdString();
    m_collectDevice.periodTime = ui->comboBox_3->currentText().toStdString();
    m_collectDevice.isDisable = ui->checkBox->checkState()==Qt::Checked?true:false;
    m_collectDevice.InterfaceType = (stCollecDevice::Intarface_Type)ui->comboBox_2->currentIndex();
    m_collectDevice.tcpClient.ipaddrA = ((QLineEdit *)m_mapChilds.value("ipaddrA"))->text().toStdString();
    m_collectDevice.tcpClient.ipaddrB = ((QLineEdit *)m_mapChilds.value("ipaddrB"))->text().toStdString();
    m_collectDevice.tcpClient.portA = ((QLineEdit *)m_mapChilds.value("portA"))->text().toStdString();
    m_collectDevice.tcpClient.portB = ((QLineEdit *)m_mapChilds.value("portB"))->text().toStdString();

    m_collectDevice.tcpServer.ipaddr = ((QLineEdit *)m_mapChilds.value("serverip"))->text().toStdString();
    m_collectDevice.tcpServer.port = ((QLineEdit *)m_mapChilds.value("serverip"))->text().toStdString();

    m_collectDevice.udp.localIpaddr = ((QLineEdit *)m_mapChilds.value("updSrcIpaddr"))->text().toStdString();
    m_collectDevice.udp.localPort = ((QLineEdit *)m_mapChilds.value("updSrcPort"))->text().toStdString();
    m_collectDevice.udp.remoteIpaddr = ((QLineEdit *)m_mapChilds.value("updDestIpaddr"))->text().toStdString();
    m_collectDevice.udp.remotePort = ((QLineEdit *)m_mapChilds.value("updDestPort"))->text().toStdString();

    for (int i = 0; i < ui->tableWidget->rowCount(); ++i) {
        stPrivateParam param;
        param.id = ui->tableWidget->item(i,0)->text().toInt();
        param.name = ui->tableWidget->item(i,1)->text().toStdString();
        param.desc = ui->tableWidget->item(i,2)->text().toStdString();
        param.value = ui->tableWidget->item(i,3)->text().toStdString();
        m_collectDevice.params.push_back(param);
    }
    return m_collectDevice;
}

void NewDeviceDialog::ParseXML(QString strFilePath)
{
    pugi::xml_document doc;
    if(!doc.load_file(strFilePath.toStdString().c_str()))
    {
        return ;
    }
    pugi::xml_node collect_node = doc.root().child("protocal");
    pugi::xml_node params_node = collect_node.child("params");
    pugi::xml_node param_node = params_node.first_child();
    while(param_node)
    {
        stPrivateParam param ;
        param.id = param_node.attribute("id").as_int();
        param.desc = param_node.attribute("desc").value();
        param.name = param_node.attribute("name").value();
        param.value = param_node.attribute("value").value();
        m_params[param.id] = param;
        param_node = param_node.next_sibling();
    }
}

void NewDeviceDialog::on_pushButton_clicked()
{
    accept();
}

void NewDeviceDialog::on_pushButton_2_clicked()
{
    close();
}

void NewDeviceDialog::on_comboBox_2_currentTextChanged(const QString &arg1)
{
    if(arg1.compare("TCP客户端")==0)
    {
        ui->stackedWidget->setCurrentIndex(0);
    }else if (arg1.compare("TCP服务端")==0)
    {
        ui->stackedWidget->setCurrentIndex(1);
    }
    else if (arg1.compare("UDP")==0)
    {
        ui->stackedWidget->setCurrentIndex(2);
    }
    else if (arg1.compare("串口")==0)
    {
        ui->stackedWidget->setCurrentIndex(3);
    }
}


